const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    typeList: ['拍照报事', '发现物种', '生态公益', '趣味科普'],
    tabActived: 0, // 当前选中的Tab页 0 : 举报事件 1：发现物种 2：活动晒照
    tabScroll: '200px', // scroll高度
    scrollY: false,
    start: 1, // 贴子开始页数
    limit: 10, // 每页条数
    total: 0 // 总条数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getActivity()
    this.setData({
      tabScroll: app.globalData.windowHeight - app.globalData.screenHeight / 1334 * (80) + 'px'
    })
    this.getColumn()
  },
  /**
   * 点击栏目
   * @params e => e.currentTarget.dataset.type
   * 跳转页面
   */
  chooseType: function (e) {
    let type = parseInt(e.currentTarget.dataset.type);
    switch (type) {
      case 1:
        wx.switchTab({
          url: '/pages/report/report',
        })
        break;
      case 2:
        wx.switchTab({
          url: '/pages/find/find',
        })
        break;
      case 3:
        wx.navigateTo({
          url: '../activity/list',
        })
        break;
      case 4:
        console.log(4)
        break;
    }
  },
  chooseTab: function (e) {
    let index = parseInt(e.currentTarget.dataset.index);
    if (index == this.data.tabActived) return
    this.setData({
      tabActived: index,
      postList: [],
      // 页码重新计数
      start: 1,
    })
    this.getColumn(true)
  },
  changeColumn: function (e) {
    let id = e.currentTarget.dataset.id
    this.setData({
      column: id,
      postList: [],
      // 页码重新计数
      start: 1,
    })
    this.getPostList(true)
  },
  /**
   * 获取活动推荐
   */
  getActivity: function() {
    app.get('activity/getList', {start: 1, limit: 2}, true)
      .then(res => {
        this.setData({
          activityList: res.list
        })
      })
  },
  /**
   * 获取栏目信息
   * @params type
   */
  getColumn: function (isLoading) {
    app.get('posts/getColumn', { type: this.data.tabActived})
      .then(res => {
        this.setData({
          columnList: res,
          column: res[0].id
        })
        this.getPostList(isLoading) 
      })
  },

  /**
   * 获取贴子
   * @params start
   * @params limit
   * @params tabActived
   */
  getPostList: function (isLoading=false) {
    app.get('posts/getList', { start: this.data.start, limit: this.data.limit, type: this.data.tabActived, column: this.data.column}, isLoading)
      .then(res => {
        let postList = res.list
        if (this.data.postList) {
          postList = [...postList, ...this.data.postList]
        }
        this.setData({
          postList: postList,
          total: res.total
        })
      })
  },
  /**
   * 获取下一页
   */
  getMore: function () {
    let start = this.data.start,
        limit = this.data.limit,
        total = this.data.total;
    if (start * limit >= total) {
      return
    } else {
      start += 1
      this.setData({
        start: start
      })
      this.getPostList(true)
    }
  },
  lookImg: function (e) {
    app.imgs(e.currentTarget.dataset.urls, e.currentTarget.dataset.src)
  },
  /**
   * 点赞
   * @params id
   */
  like: function (e) {
    let index = e.currentTarget.dataset.index,
        id = this.data.postList[index].id,
        postList = this.data.postList;
    app.get('posts/like', {id: id})
      .then(() => {
        postList[index].likes += 1;
        postList[index].isLike = 1;
        this.setData({
          postList: postList
        })
      })
  },
  goDetail: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../detail/detail?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    this.setData({
      scrollY: true
    })
  },
  changeScroll: function () {
    if (this.data.scrollY) {
      this.setData({
        scrollY: false
      })
    }
  },
  onPageScroll: function () {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})